﻿@model  Himall.Model.CouponInfo
@{
    bool CanAddIntegralCoupon = (bool)ViewBag.CanAddIntegralCoupon;
}
<link rel="stylesheet" type="text/css" href="~/Content/bootstrap-datetimepicker.min.css">
<script type="text/javascript" src="~/Scripts/bootstrap-datetimepicker.js"></script>
<script type="text/javascript" src="~/Scripts/bootstrap-datetimepicker.zh-CN.js"></script>
<script type="text/javascript">
    $(function () {
        $("#couponName").focus();

        $(".start_datetime").datetimepicker({
            language: 'zh-CN',
            format: 'yyyy-mm-dd',
            autoclose: true,
            weekStart: 1,
            minView: 2
        });
        $(".end_datetime").datetimepicker({
            language: 'zh-CN',
            format: 'yyyy-mm-dd',
            autoclose: true,
            weekStart: 1,
            minView: 2
        });

        $(".endintdatetime").datetimepicker({
            language: 'zh-CN',
            format: 'yyyy-mm-dd hh:ii:ss',
            autoclose: true,
            weekStart: 1,
            minView: 0
        });

        $('.end_datetime').datetimepicker('setStartDate', $(".start_datetime").val());
        $('.end_datetime').datetimepicker('setEndDate', "@ViewBag.EndTime");
        $('.start_datetime').on('changeDate', function () {
            if ($(".end_datetime").val()) {
                if ($(".start_datetime").val() > $(".end_datetime").val()) {
                    $('.end_datetime').val($(".start_datetime").val());
                    setMaxEndIntegralExchange($(".end_datetime").val());
                    if ($("#EndIntegralExchange").val() > $(".end_datetime").val()) {
                        $('#EndIntegralExchange').val($(".end_datetime").val());
                    }
                }
            }
            $('.end_datetime').datetimepicker('setStartDate', $(".start_datetime").val());
            GetExpiryDate();
        });
        $('.end_datetime').on('changeDate', function () {
            if ($(".end_datetime").val()) {
                $('#EndIntegralExchange').datetimepicker('setEndDate', $(".end_datetime").val());
                if ($("#EndIntegralExchange").val() > $(".end_datetime").val()) {
                    $('#EndIntegralExchange').val($(".end_datetime").val());
                }
                setMaxEndIntegralExchange($(".end_datetime").val());
            }
            GetExpiryDate();
        });

        $('#EndIntegralExchange').datetimepicker('setStartDate', "@(Model.StartTime)");
        setMaxEndIntegralExchange($(".end_datetime").val());


    });
    function setMaxEndIntegralExchange(v) {
        $('#EndIntegralExchange').datetimepicker('setEndDate', v + " 23:59:59");
    }
    function GetExpiryDate() {
        var a = $('#starttime').val(),
               b = $('#endtime').val();
        $('#id_date').html(a + '至' + b);
    }
</script>
<script src="~/Scripts/v.js"></script>
<div class="container">
    <style>
        .tip { margin-left: 0; }

        .tip-pass { display: none; }
        .form-control-static label { padding-right: 20px; }
    </style>
    <ul class="nav nav-tabs-custom clearfix">
        <strong>优惠券</strong>
        <li class=""><a href="@Url.Action("Management","Coupon")">所有优惠卷</a></li>
        <li class="active"><a>@(Model.Id > 0 ? "修改" : "新增")优惠券</a></li>
        <li class=""><a href="@Url.Action("BuyService", "Coupon")">购买服务</a></li>
        <em class="primary-btn">
            <span class="glyphicon glyphicon-question-sign"></span>
            <div class="primary">
                <h3>温馨提示：</h3>
                <span>*</span><p>显示店铺的优惠券列表</p>
            </div>
        </em>
    </ul>
    <div class="row">

        <div class="col-xs-7">
            <form role="form" class="form-horizontal" id="v-form" style="padding:10px;background:#f8f8f8;" method="post" action="@Url.Action("Edit")">
                <div class="form-group">
                    <input type="hidden" id="CouponId" name="Id" value="@Model.Id" />
                    <label class="col-sm-2 control-label" for="">优惠券名称：</label>
                    <div class="col-sm-6">
                        <input type="text" id="couponName" value="@Model.CouponName" data-d-group="0" name="couponName" class="auto form-control input-sm" id="freightInput">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label" for="">面值：</label>
                    <div class="col-sm-3">
                        <input type="text" data-a-sign="¥ " data-d-group="0" value="@Model.Price" name="price" class="auto form-control input-sm" id="price" @(Model.Id > 0 ? "disabled" : "")>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label" for="">每人限领：</label>
                    <div class="col-sm-3">
                        <select class="form-control input-sm" name="permax" data="@Model.PerMax" id="id_select">
                            <option value="0">不限张</option>
                            <option value="1">1张</option>
                            <option value="2">2张</option>
                            <option value="3">3张</option>
                            <option value="4">4张</option>
                            <option value="5">5张</option>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label" for="">订单金额：</label>
                    <div class="col-sm-4">
                        <div class="radio  @(Model.Id > 0 ? "disabled" : "")">
                            <label>
                                <input type="radio" name="orderamount" value="0" @(Model.Id > 0 ? "disabled" : "")>不限制
                            </label>
                        </div>
                        <div>
                            <label style="display:inline-block;">
                                <input type="radio" name="orderamount" id="orderamout" value="0.00" @(Model.Id > 0 ? "disabled" : "")>
                            </label>
                            <label style="display:inline-block;">
                                满<input type="text" data-a-sign="¥ " data-d-group="0" value="@Model.OrderAmount" class="auto form-control input-sm" style="display:inline-block;width:50px;margin-left:5px;margin-right:5px;" id="orderamout1" data="@Model.OrderAmount" @(Model.Id > 0 ? "disabled" : "")>元可使用
                            </label>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label" for="">发放总量：</label>
                    <div class="col-sm-3">
                        <input type="text" data-d-group="0" value="@Model.Num" name="num" maxlength="7" class="auto form-control input-sm" id="num">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label" for="">生效时间：</label>
                    <div class="col-sm-3">
                        <input type="text" data-d-group="0" value="@Model.StartTime.ToString("yyyy-MM-dd")" name="StartTime" class="auto form-control input-sm start_datetime" id="starttime" @(Model.Id > 0 ? "disabled" : "")>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label" for="">过期时间：</label>
                    <div class="col-sm-3">
                        <input type="text" data-d-group="0" value="@Model.EndTime.ToString("yyyy-MM-dd")" name="EndTime" class="auto form-control input-sm end_datetime" id="endtime" @(Model.Id > 0 ? "disabled" : "")>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label" for="">可使用商品：</label>
                    <div class="col-sm-10">
                        <p class="form-control-static">全店通用</p>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label" for="">领取方式：</label>
                    <div class="col-sm-10">
                        <p class="form-control-static">
                            <label><input name="ReceiveType" type="radio" value="0" @(((int)Model.ReceiveType) == 0 ? "checked" : "") />店铺首页</label>
                            @if (Model.Id > 0 || CanAddIntegralCoupon)
                            {
                                <label><input name="ReceiveType" type="radio" value="1" @(((int)Model.ReceiveType) == 1 ? "checked" : "") />积分兑换</label>
                            }
                            <label><input name="ReceiveType" type="radio" value="2" @(((int)Model.ReceiveType) == 2 ? "checked" : "") />主动发放</label>

                        </p>
                    </div>
                </div>

                <div class="rectypeitem">
                    <div class="form-group">
                        @{
                            var pc = @Model.Himall_CouponSetting.Any(a => a.PlatForm == Himall.Core.PlatformType.PC);
                            var wap = @Model.Himall_CouponSetting.Any(a => a.PlatForm == Himall.Core.PlatformType.Wap);
                            string pcchecked = pc ? "checked" : "";
                            string wapchecked = wap ? "checked" : "";
                        }
                        <label class="col-sm-2 control-label" for="">推广：</label>
                        <div class="col-sm-10">
                            <input id="ChkPC" name="chkShow" type="checkbox" value="PC" @pcchecked />推广至PC端店铺导航右侧
                            &nbsp;
                            <input id="ChkWap" name="chkShow" type="checkbox" value="WAP" @wapchecked @(Model.CanVshopIndex ? "" : "disabled") />推广至移动端店铺首页 @Html.Raw(Model.CanVshopIndex ? "" : "<span class=\"red\">[未开通微店]</span>")
                        </div>
                    </div>
                </div>
                @if (Model.Id > 0 || CanAddIntegralCoupon)
                {
                    <div class="rectypeitem">
                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="">所需积分：</label>
                            <div class="col-sm-3">
                                @Html.TextBoxFor(m => m.NeedIntegral, new { @class = "form-control input-sm", maxlength = "7" })
                            </div>
                            <div class="col-sm-3">
                                <span class="help-default">最低10分起兑</span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="">兑换截止：</label>
                            <div class="col-sm-3">
                                <input type="text" data-d-group="0" value="@(Model.EndIntegralExchange.HasValue ? Model.EndIntegralExchange.Value.ToString():"")" name="EndIntegralExchange" id="EndIntegralExchange" class="auto form-control input-sm endintdatetime">
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="form-group upload-img" id="imgIntegralCover"></div>
                        </div>
                    </div>
                }
                <div class="rectypeitem"> </div>

                <div class="form-group">
                    <label class="col-sm-2 control-label" for="">同步微信：</label>
                    <div class="col-sm-5">
                        <p class="form-control-static">
                            <label>@Html.CheckBoxFor(m=>m.FormIsSyncWeiXin) 同步到微信卡券</label>
                        </p>
                    </div>
                    <div class="col-sm-5">
                        <p class="form-control-static" style="color:#999;">如果您的微信公众号未开通卡券权限，将由平台公众号代发卡券。同步到微信卡包后，需要等待微信审核通过才能领取该券。</p>
                    </div>
                </div>
                <div id="syncwxbox" style="@(Model.IsSyncWeiXin == 1?"":"display:none;")">
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="">卡券颜色：</label>
                        <div class="col-sm-5">
                            <p class="form-control-static">
                                <select name="FormWXColor" id="FormWXColor">
                                    <option value="">选择卡券颜色</option>
                                    @foreach (System.Collections.DictionaryEntry item in Himall.Model.WXCardLogInfo.WXCardColors)
                                    {
                                        <option value="@(item.Key)" style="background:@(item.Value);color:#fff;">@(item.Key)</option>
                                    }
                                </select>
                            </p>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="">卡券标题：</label>
                        <div class="col-sm-5">
                            <p class="form-control-static">
                                <input type="text" value="@(Model.WXCardInfo != null ? Model.WXCardInfo.CardTitle: "")" name="FormWXCTit" maxlength="18" class="auto form-control input-sm" id="FormWXCTit">
                            </p>
                        </div>
                        <div class="col-sm-5">
                            <p class="form-control-static" style="color:#999;">建议填写代金券“减免金额”及自定义内容，描述卡券提供的具体优惠。例如：HiMall 100元代金券;字数上限为9个汉字</p>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="">卡券副标题：</label>
                        <div class="col-sm-5">
                            <p class="form-control-static">
                                <input type="text" value="@(Model.WXCardInfo != null ? Model.WXCardInfo.CardSubTitle: "")" name="FormWXCSubTit" maxlength="36" class="auto form-control input-sm" id="FormWXCSubTit">
                            </p>
                        </div>
                        <div class="col-sm-5">
                            <p class="form-control-static" style="color:#999;">字数上限为18个汉字</p>
                        </div>
                    </div>

                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button class="btn btn-primary" type="submit">保 存</button>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label" for="">温馨提示：</label>
                    <div class="col-sm-10">
                        <p class="form-control-static" style="color:#999;">分别推广到PC端及移动端店铺首页的优惠券不能超过五张</p>
                    </div>
                </div>
            </form>
        </div>
        <div class="col-xs-4 fr">
            <div id="id_title" style="background:#FF7676;border-bottom:1px dashed #e4e4e4;height:45px;text-align:center;line-height:45px;padding:0 10px;color:#fff;">
                @Model.CouponName
            </div>
            <div style="padding:0 10px;background:#FF7676;border-bottom:1px dashed #e4e4e4;height:45px;text-align:center;line-height:45px;font-size:16px;color:#f60;font-weight:700;color:#fff;" id="id_price">¥@Model.Price</div>
            <div style="padding:0 10px;background:#FF7676;color:#666;height:45px;line-height:45px;color:#fff;">
                有效日期：<span style="display:inline-block;color:#fff;" id="id_date"></span>
            </div>
            <div style="padding:0 10px;color:#666;height:45px;line-height:45px;background:#FFCD66;color:#fff;">
                <div id="limit">
                    使用限制：
                    @if (Model.OrderAmount == 0)
                    {
                        <span>无限制</span>
                    }
                    else
                    {
                        <span>订单满<span style="color:#f60;display:inline-block;" id="id_p">@Model.OrderAmount</span>元可使用（不含运费）</span>
                    }
                </div>
            </div>
        </div>

    </div>
</div>
<script src="~/Scripts/jquery.himallUpload.js"></script>
<script>
    var d_rectype = $("input[name = 'ReceiveType']");
    var d_rectypecon = $(".rectypeitem");
    $(function () {
        $("#imgIntegralCover").himallUpload(
        {
            title: '封面图片：',
            imageDescript: '非必填，建议尺寸 150 *120；默认显示商家logo',
            displayImgSrc: "@(Model.IntegralCover)",
            headerWidth: 2,
            dataWidth: 9,
            imgFieldName: "IntegralCover"
        });
        d_rectype.click(function () {
            var _t = $(this);
            var _ind = d_rectype.index(_t);
            showrectypetab(_ind);
        });

        //初始显示
        var currectype = $("input[name = 'ReceiveType']:checked");
        var curind = d_rectype.index(currectype);
        showrectypetab(curind);

        $("input[name='orderamount']").focus(function () {
            var _t = $(this);
            var v = _t.val();
            try {
                v = parseFloat(v);
            } catch (ex) {
                v = 0;
            }
            showlimittxt(v);
        });

        $("#FormIsSyncWeiXin").click(function () {
            var _t = $(this);
            var syncwxbox = $("#syncwxbox");
            if (_t.is(":checked")) {
                syncwxbox.show();
            } else {
                syncwxbox.hide();
            }
        });
    });

    function showrectypetab(n) {
        d_rectypecon.hide();
        d_rectypecon.eq(n).show();
    }

    function showlimittxt(v) {
        if (v != 0) {
            $("#limit").html('使用限制：<span>订单满<span style="color:#f60;display:inline-block;" id="id_p">' + v + '</span>元可使用（不含运费）</span>');
        } else {
            $("#limit").html('使用限制：<span>无限制</span>');
        }
    }

    var money = parseFloat('@Model.OrderAmount').toFixed(2);

    (function () {
        if (money == 0) {
            $('input[name="orderamount"]').eq(0).attr('checked', 'true');
        } else {
            $('input[name="orderamount"]').eq(1).attr('checked', 'true');
        }
        ;
        var a = $('#orderamout1').attr('data'),
            b = $('#id_select').attr('data');
        $('#id_select').children().each(function (i, e) {
            if ($(e).val() == b) {
                $(e).attr('selected', true);
                return false;
            }
        });
        GetExpiryDate();
    }());
    var a = v({
        form: 'v-form',// 表单id 必须
        beforeSubmit: function () {
            loadingobj = showLoading();
        },
        afterSubmit: function (data) {
            loadingobj.close();
            if (data.success) {
                a.reset();
                $.dialog.succeedTips('数据提交成功！', function () { window.location.href = '/SellerAdmin/Coupon/Management'; });

            } else {
                $.dialog.errorTips('数据提交失败,' + data.msg);
            }
        },// 表单提交之后的回调 不是必须
        ajaxSubmit: true// 是否ajax提交 如果没有这个参数那么就是默认提交方式 如果没有特殊情况建议默认提交方式
    });
    a.add(
        {
            target: "couponName",
            afterBlur: function () {
                $('#id_title').html($('#couponName').val());
            },

            ruleType: "required&&ce",
            tips: "1~20个字符，由中文、英文字母和数字组成。",
            error: "对不起，优惠券名称格式不正确。",
        },
        {
            target: "price",
            ruleType: "required&&uint",
            afterBlur: function () {
                $('#id_price').html('¥' + $('#price').val());
            },
            fnRule: function () {
                var a = $('#price').val();
                if (a.length > 3 || a - 0 == 0) {
                    return false;
                }
            },
            tips: '该信息为必填项，请输入金额!',
            error: '金额输入错误,三位以下且大于0的整数!'
        },

        {
            target: "num",
            ruleType: "required&&uint",
            fnRule: function () {
                var a = $('#num').val();
                if (a <= 0 || a > 999999) {
                    return false;
                }
            },
            afterBlur: function () {
                $('#id_price').html('¥' + $('#price').val());
            },
            tips: '该信息为必填项，请输入发放数量!',
            error: '1-999999之间!'
        },
        {
            target: "orderamout1",
            ruleType: "money||empty",
            fnRule: function () {
                var a = $('#orderamout1').val(),
                    p = $('input[name="orderamount"]').attr('checked'),
                    b = $('#price').val();
                if (p) { return true; }
                if (a - 999999 > 0) { return false; }
                if (a != 0 && b - a > 0) {
                    return false;
                }
            },
            beforeFocus: function () {
                $('#orderamout').attr('checked', 'true');
            },
            afterBlur: function () {
                var a = $('#orderamout1').val();
                $('#orderamout').attr('value', a);
                $('#id_p').html('¥' + a);
                showlimittxt(a);
            },
            tips: '该信息为必填项，请输入金额!',
            error: '错误,必须大于优惠券面额,小于999999！'
        },

        {
            target: "starttime",
            ruleType: "required&&date",
            tips: '该信息为必填项，请选择时间!',
            error: '时间格式错误!'
        },
        {
            target: "endtime",
            ruleType: "required&&date",
            tips: '该信息为必填项，请选择时间!',
            error: '时间格式错误!'
        }
    );
</script>
